/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.controllers;

import com.ez2find.backing.ForumsSession;
import com.ez2find.manager.ForumManagerLocal;
import com.ez2find.manager.MessageManagerLocal;
import com.ez2find.manager.TopicManagerLocal;
import com.ez2find.model.Forum;
import com.ez2find.model.Message;
import com.ez2find.model.Topic;
import com.ez2find.util.SessionContext;
import java.io.*;
import java.net.*;

import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrador
 */
public class linkReply extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    @EJB
    ForumManagerLocal ejbForums;
    @EJB
    TopicManagerLocal ejbThreads;
    @EJB
    MessageManagerLocal ejbMessages;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        Forum forum = ejbForums.find(Long.parseLong(request.getParameter("forum")));
        Topic thread = ejbThreads.find(Long.parseLong(request.getParameter("thread")));
        Message message = ejbMessages.find(Long.parseLong(request.getParameter("message")));

        if (forum != null && thread != null && message != null) {
            ForumsSession fBean = (ForumsSession) SessionContext.getAttribute("ForumsSession");
            if (fBean != null) {
                fBean.setCurrentForum(forum);
                fBean.setCurrentThread(thread);
                fBean.setCurrentMessage(message);
            } else {
                ForumsSession newfBean = new ForumsSession();
                newfBean.setCurrentForum(forum);
                newfBean.setCurrentThread(thread);
                newfBean.setCurrentMessage(message);

                SessionContext.setAttribute("ForumsSession", newfBean);
            }
            
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
                    String errorPageURL = externalContext.getRequestContextPath() + "/faces/forum/ffound.jsp";
                    externalContext.redirect(errorPageURL);
            } else {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head><title>ez2find</title></head>");
            out.println("<body>");
            out.println("<h2>Error</h2>");
            out.println("<h4>Your session expired or this is an invalid url</h4>");
            out.println("<a href=\"/site-es/faces/welcome.jsp\" >Go to eez2find.com</a>");
            out.println("</body></html>");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
